/*
	Simple Software Renderer (SSRenderer)
	Copyright (C) 2007 Piotr Orzechowski <piotr.orzechowski@gmail.com>

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
#include "./HPTimer.h"

namespace util
{

#ifdef WIN32

bool HPTimer::Initialize()
{
	if (!QueryPerformanceFrequency(&m_TicksPerSecond))
	{
		// System doesn't support hi-res timer
		return false;
	}
	else
	{
		QueryPerformanceCounter(&m_StartTime);
		m_bInitialized = true;
		return true;
	}
}

void HPTimer::Deinitialize()
{
	m_bInitialized = false;
}

float HPTimer::GetElapsedSeconds()
{
	if (!m_bInitialized) return 0.0f;

	LARGE_INTEGER currentTime;
	QueryPerformanceCounter(&currentTime);

	float seconds =  ((float)currentTime.QuadPart - (float)m_StartTime.QuadPart) / (float)m_TicksPerSecond.QuadPart;

	return seconds;
}

// -------------------------------------------------------------------------------- //
// -------------------------------------------------------------------------------- //
// -------------------------------------------------------------------------------- //

#else

///@todo przetestowac czy w ogole dziala ?

bool HPTimer::Initialize()
{
	timeval t;
	gettimeofday(&t,0);
	m_StartTime = ((long long)t.tv_sec) * 1000 + t.tv_usec/1000;

	m_bInitialized = true;
}

void HPTimer::Deinitialize()
{
	m_bInitialized = false;
}

float HPTimer::GetElapsedSeconds()
{
	if (!m_bInitialized) return 0.0f;

	timeval t;
	gettimeofday(&t,0);
	return ((((long long)t.tv_sec) * 1000 + t.tv_usec/1000) - m_StartTime) * 0.01f;
}

#endif

} // namespace

